<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RocketMQ主从架构深度解析 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 700;
            color: #2d3748;
        }
        .hero {
            background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 100%);
            color: white;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .architecture-diagram {
            background-color: #f8fafc;
            border-radius: 0.5rem;
            padding: 1.5rem;
        }
        .feature-icon {
            font-size: 2rem;
            color: #3b82f6;
        }
        .highlight-box {
            border-left: 4px solid #3b82f6;
            background-color: #f8fafc;
        }
        .footer {
            background-color: #1a202c;
            color: #e2e8f0;
        }
        .footer a:hover {
            color: #63b3ed;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-6xl text-center">
            <div class="flex justify-center mb-6">
                <span class="bg-blue-600 text-white px-4 py-1 rounded-full text-sm font-semibold">分布式消息队列</span>
            </div>
            <h1 class="text-4xl md:text-5xl font-bold mb-4 leading-tight">RocketMQ主从架构深度解析</h1>
            <p class="text-xl md:text-2xl text-blue-100 mb-8 max-w-3xl mx-auto">
                高可用消息系统的核心架构设计与实现原理
            </p>
            <div class="flex flex-wrap justify-center gap-4 mt-8">
                <div class="flex items-center bg-blue-900 bg-opacity-50 px-4 py-2 rounded-lg">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>高可用性</span>
                </div>
                <div class="flex items-center bg-blue-900 bg-opacity-50 px-4 py-2 rounded-lg">
                    <i class="fas fa-database mr-2"></i>
                    <span>数据可靠性</span>
                </div>
                <div class="flex items-center bg-blue-900 bg-opacity-50 px-4 py-2 rounded-lg">
                    <i class="fas fa-bolt mr-2"></i>
                    <span>高性能</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="prose max-w-none">
                <p class="text-lg text-gray-700 leading-relaxed">
                    RocketMQ的主从架构是保证消息可靠性和高可用性的关键机制，它通过在多个节点上复制消息数据，确保在主节点故障时，从节点可以接管服务，避免消息丢失和服务中断。
                </p>
            </div>
        </section>

        <!-- Architecture Modes -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <i class="fas fa-project-diagram mr-3 feature-icon"></i>
                <span>主从架构模式</span>
            </h2>
            <p class="text-gray-600 mb-8">RocketMQ支持多种主从部署模式，以满足不同的可靠性和性能需求：</p>

            <!-- Single Master -->
            <div class="card bg-white rounded-lg p-6 mb-8">
                <h3 class="text-2xl font-bold mb-4 text-blue-700">1.1 单Master模式</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-lg font-semibold mb-2 text-gray-800">特点：</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>只有一个Master节点，没有Slave节点</li>
                            <li>简单易部署，适合开发测试环境</li>
                            <li>存在单点风险，Master宕机后服务不可用</li>
                        </ul>
                    </div>
                    <div class="architecture-diagram">
                        <div class="mermaid">
                            graph TD
                                A[Master Broker]
                        </div>
                    </div>
                </div>
            </div>

            <!-- Multi Master -->
            <div class="card bg-white rounded-lg p-6 mb-8">
                <h3 class="text-2xl font-bold mb-4 text-blue-700">1.2 多Master模式</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-lg font-semibold mb-2 text-gray-800">特点：</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>多个Master节点，每个Master独立工作，没有Slave节点</li>
                            <li>每个Master负责不同的Topic分片</li>
                            <li>单个Master宕机只影响部分Topic服务</li>
                            <li>无法保证消息不丢失，但服务可用性较高</li>
                        </ul>
                    </div>
                    <div class="architecture-diagram">
                        <div class="mermaid">
                            graph TD
                                A[Master1 Broker] --> B[Master2 Broker]
                        </div>
                    </div>
                </div>
            </div>

            <!-- Multi Master Multi Slave (Async) -->
            <div class="card bg-white rounded-lg p-6 mb-8">
                <h3 class="text-2xl font-bold mb-4 text-blue-700">1.3 多Master多Slave模式（异步复制）</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-lg font-semibold mb-2 text-gray-800">特点：</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>每个Master配置一个或多个Slave</li>
                            <li>Master向Slave异步复制消息</li>
                            <li>Master宕机时，消费者可以从Slave读取消息，但可能丢失部分数据</li>
                            <li>性能较高，可靠性一般</li>
                        </ul>
                    </div>
                    <div class="architecture-diagram">
                        <div class="mermaid">
                            graph TD
                                A[Master1 Broker] --> B[Slave1 Broker]
                                C[Master2 Broker] --> D[Slave2 Broker]
                        </div>
                    </div>
                </div>
            </div>

            <!-- Multi Master Multi Slave (Sync) -->
            <div class="card bg-white rounded-lg p-6 mb-8">
                <h3 class="text-2xl font-bold mb-4 text-blue-700">1.4 多Master多Slave模式（同步双写）</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-lg font-semibold mb-2 text-gray-800">特点：</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>每个Master配置一个或多个Slave</li>
                            <li>Master向Slave同步复制消息，等待Slave确认后才返回成功</li>
                            <li>Master宕机时，消费者可以从Slave读取消息，数据零丢失</li>
                            <li>性能较低，可靠性最高</li>
                        </ul>
                    </div>
                    <div class="architecture-diagram">
                        <div class="mermaid">
                            graph TD
                                A[Master1 Broker] -->|同步复制| B[Slave1 Broker]
                                C[Master2 Broker] -->|同步复制| D[Slave2 Broker]
                        </div>
                    </div>
                </div>
            </div>

            <!-- Dledger Mode -->
            <div class="card bg-white rounded-lg p-6">
                <h3 class="text-2xl font-bold mb-4 text-blue-700">1.5 Dledger模式（推荐）</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-lg font-semibold mb-2 text-gray-800">特点：</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>基于Raft协议实现的高可用模式</li>
                            <li>自动选举Leader，无需手动切换</li>
                            <li>支持自动故障转移</li>
                            <li>数据强一致性，可靠性高</li>
                        </ul>
                    </div>
                    <div class="architecture-diagram">
                        <div class="mermaid">
                            graph TD
                                A[Broker1 Leader] --> B[Broker2 Follower]
                                A --> C[Broker3 Follower]
                                B --> C
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Node Configuration -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <i class="fas fa-cog mr-3 feature-icon"></i>
                <span>主从节点配置</span>
            </h2>

            <div class="grid md:grid-cols-2 gap-8">
                <!-- Role Configuration -->
                <div class="card bg-white rounded-lg p-6">
                    <h3 class="text-2xl font-bold mb-4 text-blue-700">2. 主从节点角色配置</h3>
                    <p class="text-gray-700 mb-4">RocketMQ通过配置文件中的<code>brokerRole</code>参数来定义Broker的角色：</p>
                    <div class="highlight-box p-4 rounded bg-gray-50 mb-4">
                        <pre class="text-sm text-gray-800">
# Master节点配置
brokerRole=ASYNC_MASTER  # 异步复制Master
# 或
brokerRole=SYNC_MASTER   # 同步复制Master

# Slave节点配置
brokerRole=SLAVE</pre>
                    </div>
                </div>

                <!-- Relation Configuration -->
                <div class="card bg-white rounded-lg p-6">
                    <h3 class="text-2xl font-bold mb-4 text-blue-700">3. 主从节点关联配置</h3>
                    <p class="text-gray-700 mb-4">主从节点通过相同的<code>brokerName</code>和不同的<code>brokerId</code>关联起来：</p>
                    <div class="highlight-box p-4 rounded bg-gray-50">
                        <pre class="text-sm text-gray-800">
# Master节点配置
brokerName=broker-a
brokerId=0  # Master的brokerId必须为0

# Slave节点配置
brokerName=broker-a  # 与Master相同的brokerName
brokerId=1  # Slave的brokerId必须大于0</pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Network Configuration -->
        <section class="mb-16">
            <h3 class="text-2xl font-bold mb-4 text-blue-700">4. 主从网络配置</h3>
            <div class="highlight-box p-6 rounded bg-gray-50">
                <pre class="text-sm text-gray-800">
# Master节点配置
listenPort=10911  # 监听端口
haListenPort=10912  # HA端口，用于主从同步

# Slave节点配置
listenPort=11911  # 监听端口
haListenPort=11912  # HA端口
masterAddress=192.168.1.1:10911  # Master地址</pre>
            </div>
        </section>

        <!-- Replication Mechanism -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <i class="fas fa-exchange-alt mr-3 feature-icon"></i>
                <span>主从复制的实现机制</span>
            </h2>
            <p class="text-gray-600 mb-8">RocketMQ的主从复制是基于HAService（High Availability Service）实现的，它负责在Master和Slave之间同步消息数据。</p>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-2xl font-bold mb-4 text-blue-700">1.1 HAService架构</h3>
                    <p class="text-gray-700 mb-4">HAService主要由以下组件组成：</p>
                    <ul class="list-disc pl-5 space-y-2 text-gray-700">
                        <li><strong>HAService</strong>：主从复制服务的主体</li>
                        <li><strong>AcceptSocketService</strong>：Master端接收Slave连接的服务</li>
                        <li><strong>GroupTransferService</strong>：Master端的消息传输服务</li>
                        <li><strong>HAClient</strong>：Slave端连接Master的客户端</li>
                    </ul>
                </div>
                <div>
                    <h3 class="text-2xl font-bold mb-4 text-blue-700">1.2 复制流程</h3>
                    <ol class="list-decimal pl-5 space-y-2 text-gray-700">
                        <li><strong>建立连接</strong>：Slave启动时，HAClient连接到Master的HAService</li>
                        <li><strong>确定复制起点</strong>：Slave向Master发送当前的最大物理偏移量（maxPhyOffset）</li>
                        <li><strong>数据传输</strong>：Master读取CommitLog中从确定点开始的数据并传输给Slave</li>
                        <li><strong>心跳维护</strong>：Master和Slave之间定期交换心跳信息</li>
                    </ol>
                </div>
            </div>

            <!-- Sync vs Async Replication -->
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6 text-blue-700">2. 同步复制与异步复制</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <!-- Sync Replication -->
                    <div class="card bg-white rounded-lg p-6">
                        <h4 class="text-xl font-bold mb-3 text-blue-600">2.1 同步复制（SYNC_MASTER）</h4>
                        <h5 class="font-semibold mb-2 text-gray-800">工作流程：</h5>
                        <ol class="list-decimal pl-5 space-y-1 text-gray-700 text-sm mb-4">
                            <li>Producer发送消息到Master</li>
                            <li>Master将消息写入本地CommitLog</li>
                            <li>Master将消息复制到Slave</li>
                            <li>Slave确认接收并写入本地CommitLog</li>
                            <li>Master收到Slave的确认</li>
                            <li>Master向Producer返回发送成功响应</li>
                        </ol>
                        <h5 class="font-semibold mb-2 text-gray-800">特点：</h5>
                        <ul class="list-disc pl-5 space-y-1 text-gray-700 text-sm">
                            <li>强一致性，确保消息在Master和Slave上都有副本</li>
                            <li>性能较低，因为需要等待Slave确认</li>
                            <li>适用于对数据一致性要求高的场景</li>
                        </ul>
                    </div>

                    <!-- Async Replication -->
                    <div class="card bg-white rounded-lg p-6">
                        <h4 class="text-xl font-bold mb-3 text-blue-600">2.2 异步复制（ASYNC_MASTER）</h4>
                        <h5 class="font-semibold mb-2 text-gray-800">工作流程：</h5>
                        <ol class="list-decimal pl-5 space-y-1 text-gray-700 text-sm mb-4">
                            <li>Producer发送消息到Master</li>
                            <li>Master将消息写入本地CommitLog</li>
                            <li>Master向Producer返回发送成功响应</li>
                            <li>Master异步将消息复制到Slave</li>
                            <li>Slave确认接收并写入本地CommitLog</li>
                        </ol>
                        <h5 class="font-semibold mb-2 text-gray-800">特点：</h5>
                        <ul class="list-disc pl-5 space-y-1 text-gray-700 text-sm">
                            <li>最终一致性，Master和Slave的数据可能存在短暂不一致</li>
                            <li>性能较高，不需要等待Slave确认</li>
                            <li>适用于对性能要求高，可以容忍短时间数据不一致的场景</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Data Sync Mechanism -->
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6 text-blue-700">3. 数据同步机制</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">3.1 CommitLog同步</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li><strong>增量同步</strong>：Slave只同步自己缺少的部分</li>
                            <li><strong>批量传输</strong>：Master会将多个消息打包一起发送</li>
                            <li><strong>流控机制</strong>：当Master和Slave之间的差距过大时，会触发流控</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">3.2 索引文件同步</h4>
                        <p class="text-gray-700 mb-2">RocketMQ不直接同步ConsumeQueue和IndexFile，而是在Slave端通过重放CommitLog来重建这些索引文件：</p>
                        <ol class="list-decimal pl-5 space-y-1 text-gray-700 text-sm">
                            <li>Slave接收并写入CommitLog数据</li>
                            <li>Slave的ReputMessageService服务解析CommitLog</li>
                            <li>根据解析结果重建ConsumeQueue和IndexFile</li>
                        </ol>
                    </div>
                </div>
            </div>

            <!-- Performance Optimization -->
            <div>
                <h3 class="text-2xl font-bold mb-4 text-blue-700">4. 复制性能优化</h3>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white p-4 rounded-lg shadow-sm">
                        <div class="text-blue-500 mb-2"><i class="fas fa-memory text-2xl"></i></div>
                        <h4 class="font-semibold mb-2">传输缓冲区优化</h4>
                        <p class="text-sm text-gray-600">使用直接内存（DirectBuffer）减少内存拷贝，批量传输减少网络开销</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-sm">
                        <div class="text-blue-500 mb-2"><i class="fas fa-hdd text-2xl"></i></div>
                        <h4 class="font-semibold mb-2">异步刷盘</h4>
                        <p class="text-sm text-gray-600">Slave可以配置异步刷盘提高性能，通过<code>flushDiskType</code>参数配置</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-sm">
                        <div class="text-blue-500 mb-2"><i class="fas fa-network-wired text-2xl"></i></div>
                        <h4 class="font-semibold mb-2">网络优化</h4>
                        <p class="text-sm text-gray-600">主从节点最好部署在同一个局域网内，使用高速网络连接减少网络延迟</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Failure Handling -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <i class="fas fa-heartbeat mr-3 feature-icon"></i>
                <span>故障转移的处理流程</span>
            </h2>
            <p class="text-gray-600 mb-8">当Master节点发生故障时，RocketMQ需要进行故障转移，确保服务的可用性。</p>

            <!-- Failure Detection -->
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6 text-blue-700">1. 故障检测</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">1.1 心跳检测</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>NameServer通过心跳机制检测Broker状态</li>
                            <li>Broker每30秒向所有NameServer发送心跳</li>
                            <li>NameServer每10秒检查一次Broker心跳</li>
                            <li>如果120秒没有收到心跳，NameServer认为Broker不可用</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">1.2 客户端检测</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>Producer和Consumer也会感知Broker状态</li>
                            <li>当发送或消费请求失败时，会尝试其他Broker</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Read Failover -->
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6 text-blue-700">2. 读故障转移</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">2.1 消费者故障转移</h4>
                        <p class="text-gray-700 mb-2">当Master不可用时，消费者可以从Slave读取消息：</p>
                        <ol class="list-decimal pl-5 space-y-1 text-gray-700 text-sm mb-4">
                            <li>Consumer向Master请求消息失败</li>
                            <li>Consumer查询NameServer获取Slave信息</li>
                            <li>Consumer切换到Slave读取消息</li>
                        </ol>
                        <div class="highlight-box p-4 rounded bg-gray-50">
                            <pre class="text-sm text-gray-800">
# 是否允许从Slave读取消息，默认为false
slaveReadEnable=true</pre>
                        </div>
                    </div>
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">2.2 读负载均衡</h4>
                        <p class="text-gray-700 mb-2">RocketMQ支持将读请求分散到Slave节点，减轻Master负担：</p>
                        <div class="highlight-box p-4 rounded bg-gray-50">
                            <pre class="text-sm text-gray-800">
# Broker权限设置，可读可写为6，只读为4，只写为2
brokerPermission=6  # Master可读可写

# Slave配置
brokerPermission=4  # Slave只读</pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Write Failover -->
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6 text-blue-700">3. 写故障转移</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <!-- Traditional Master-Slave -->
                    <div class="card bg-white rounded-lg p-6">
                        <h4 class="text-xl font-bold mb-3 text-blue-600">3.1 传统主从模式</h4>
                        <p class="text-gray-700 mb-2">在传统的主从模式下，写故障转移需要手动干预：</p>
                        <ol class="list-decimal pl-5 space-y-1 text-gray-700 text-sm mb-4">
                            <li>管理员发现Master故障</li>
                            <li>手动将一个Slave提升为Master</li>
                            <li>修改相关配置并重启</li>
                            <li>更新NameServer中的路由信息</li>
                        </ol>
                        <p class="text-gray-700 text-sm">这种方式操作复杂，恢复时间长，不适合生产环境。</p>
                    </div>

                    <!-- Dledger Mode -->
                    <div class="card bg-white rounded-lg p-6">
                        <h4 class="text-xl font-bold mb-3 text-blue-600">3.2 Dledger模式（自动故障转移）</h4>
                        <p class="text-gray-700 mb-2">RocketMQ 4.5版本引入的Dledger模式支持自动故障转移：</p>
                        <ul class="list-disc pl-5 space-y-1 text-gray-700 text-sm">
                            <li><strong>Leader选举</strong>：基于Raft协议自动选举新的Leader</li>
                            <li><strong>角色切换</strong>：新Leader自动接管写服务，无需手动干预</li>
                            <li><strong>数据同步</strong>：新Leader确保数据同步到多数节点后才提供服务</li>
                        </ul>
                        <div class="highlight-box p-4 rounded bg-gray-50 mt-4">
                            <pre class="text-sm text-gray-800">
# 所有节点通用配置
enableDLegerCommitLog=true
dLegerGroup=broker-group
dLegerPeers=n0-192.168.1.1:40911;n1-192.168.1.2:40911;n2-192.168.1.3:40911

# 节点1配置
dLegerSelfId=n0

# 节点2配置
dLegerSelfId=n1

# 节点3配置
dLegerSelfId=n2</pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Data Consistency -->
            <div>
                <h3 class="text-2xl font-bold mb-6 text-blue-700">4. 数据一致性保证</h3>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white p-4 rounded-lg shadow-sm">
                        <div class="text-blue-500 mb-2"><i class="fas fa-sync-alt text-2xl"></i></div>
                        <h4 class="font-semibold mb-2">同步复制模式</h4>
                        <p class="text-sm text-gray-600">只有当消息成功复制到多数节点后，才返回成功。Master故障时，已确认的消息在Slave上有完整副本，保证数据零丢失。</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-sm">
                        <div class="text-blue-500 mb-2"><i class="fas fa-clock text-2xl"></i></div>
                        <h4 class="font-semibold mb-2">异步复制模式</h4>
                        <p class="text-sm text-gray-600">Master确认消息后立即返回，异步复制到Slave。Master故障时，可能有部分消息未复制到Slave，可能导致少量消息丢失。</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-sm">
                        <div class="text-blue-500 mb-2"><i class="fas fa-check-double text-2xl"></i></div>
                        <h4 class="font-semibold mb-2">Dledger模式</h4>
                        <p class="text-sm text-gray-600">基于Raft协议，只有当消息提交到多数节点后才确认。自动选举拥有最新数据的节点作为新Leader，保证数据强一致性。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Best Practices -->
        <section>
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <i class="fas fa-star mr-3 feature-icon"></i>
                <span>主从架构的最佳实践</span>
            </h2>

            <!-- Deployment Recommendations -->
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6 text-blue-700">1. 部署建议</h3>
                <div class="grid md:grid-cols-3 gap-8">
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">1.1 硬件配置</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li><strong>Master节点</strong>：高性能CPU、大内存、SSD存储</li>
                            <li><strong>Slave节点</strong>：配置可以略低于Master，但建议保持一致</li>
                            <li><strong>网络</strong>：主从节点间使用高速网络连接，最好是万兆网络</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">1.2 部署模式选择</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li><strong>开发测试环境</strong>：单Master模式或多Master模式</li>
                            <li><strong>一般生产环境</strong>：多Master多Slave异步复制模式</li>
                            <li><strong>核心业务生产环境</strong>：多Master多Slave同步复制模式或Dledger模式</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">1.3 节点分布</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>主从节点应部署在不同的物理机上</li>
                            <li>考虑跨机架或跨机房部署，提高容灾能力</li>
                            <li>Dledger模式建议部署奇数个节点（如3个或5个）</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Monitoring & Ops -->
            <div class="mb-12">
                <h3 class="text-2xl font-bold mb-6 text-blue-700">2. 监控与运维</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">2.1 关键监控指标</h4>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li><strong>主从同步延迟</strong>：Master和Slave之间的数据差距</li>
                            <li><strong>心跳状态</strong>：各节点的心跳是否正常</li>
                            <li><strong>磁盘使用率</strong>：存储空间使用情况</li>
                            <li><strong>网络流量</strong>：主从之间的网络传输情况</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-xl font-bold mb-3 text-blue-600">2.2 常见问题处理</h4>
                        <div class="space-y-4">
                            <div>
                                <h5 class="font-semibold mb-1 text-gray-800">主从同步中断：</h5>
                                <ul class="list-disc pl-5 text-sm text-gray-700">
                                    <li>检查网络连接</li>
                                    <li>检查磁盘空间</li>
                                    <li>重启Slave节点重新同步</li>
                                </ul>
                            </div>
                            <div>
                                <h5 class="font-semibold mb-1 text-gray-800">数据不一致：</h5>
                                <ul class="list-disc pl-5 text-sm text-gray-700">
                                    <li>停止服务</li>
                                    <li>清空Slave数据</li>
                                    <li>从Master重新同步所有数据</li>
                                </ul>
                            </div>
                            <div>
                                <h5 class="font-semibold mb-1 text-gray-800">Master故障：</h5>
                                <ul class="list-disc pl-5 text-sm text-gray-700">
                                    <li>传统模式：手动提升Slave为Master</li>
                                    <li>Dledger模式：自动选举新Leader</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Performance Optimization -->
            <div>
                <h3 class="text-2xl font-bold mb-6 text-blue-700">3. 性能优化</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li><strong>复制模式选择</strong>：对性能要求高的场景，选择异步复制；对数据一致性要求高的场景，选择同步复制</li>
                            <li><strong>刷盘策略</strong>：Master和Slave可以使用不同的刷盘策略，例如Master同步刷盘，Slave异步刷盘</li>
                        </ul>
                    </div>
                    <div>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li><strong>网络优化</strong>：增加网络带宽，优化网络配置，减少延迟</li>
                            <li><strong>负载均衡</strong>：启用Slave读功能，分担Master读压力；合理分配Topic到不同的Master</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="footer py-8">
        <div class="container mx-auto max-w-6xl px-4 text-center">
            <div class="mb-4">
                <h3 class="text-xl font-semibold mb-2">技术小馆</h3>
                <p class="text-blue-300">深度技术文章与架构实践</p>
            </div>
            <div>
                <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-blue-100 transition-colors">
                    <i class="fas fa-globe mr-1"></i> http://www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            },
            themeVariables: {
                primaryColor: '#E5E7EB',
                primaryBorderColor: '#1E40AF',
                primaryTextColor: '#1F2937',
                lineColor: '#1E40AF'
            }
        });
    </script>
</body>
</html>